<?php namespace Admin\Controller; 

use Hdphp\Controller\Controller;
//商品列表控制器
class GoodslistController extends CommonController{
	private $model;
	private $gattrModel;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化kindgoods模型类
		$this->model = new \Common\Model\Goodslist;
		$this->gattrModel = new \Common\Model\Goodssx;
	}
	public function index(){
		
		$gid = Q('get.gid',0,'intval');	
		$type_tid = Db::table('goods') ->where("gid={$gid}")->pluck('type_tid');		
		//类型属性数据
		$tyattr = Db::table('nature')->where("type_tid={$type_tid} and natype=0 ")->get();																																																																										
//		p($tyattr);die;
		//获得tid
		$nid = Db::table('nature')->where("type_tid={$type_tid} and natype=0 ")->pluck('nid');
//		p($nid);die;
		//商品属性数据
		$gattr = Db::table('goodssx')->where("goods_gid={$gid} and nature_nid={$nid}")->get();
//		p($gattr);die;
	
	

	
	
	
	
		$attr=array();
		foreach ($tyattr as $k => $v) {
			
			$arr['naname']= $v['naname'];
			$arr['nazhi']=Db::table('goodssx')->where('nature_nid='.$v['nid'])->get();
			$attr[]=$arr;
		}
//		p($attr);die;
		View::with('attr',$attr);
		//p($attr);
		//查询货品列表数据用于页面展示
		$kindData = $this->model->where("goods_gid=$gid")->get();
//		p($kindData);die;
	

		
	
		foreach ($kindData as $k => $v) {
			//先将字符串拆分成数组
			$arr = explode('|', $v['groupid']);
			//p($arr);
			foreach ($arr as $key => $value) {
				//查询商品属性表取得$value（49，52）所对应的属性值再赋值给$arr数组
				$arr[$key] = Db::table('goodssx')->where("gsid={$value}")->pluck('sxzhi');
			}
			//再将$arr数组赋给大数组
			$kindData[$k]['groupid'] = $arr;
		}
		//p($kindData);
	
		$spec = Db::table('goodssx')->where("goods_gid={$gid}")->get();
		View::with('kindData',$kindData);
	    View::make();
	
	
	
	
	
	
	}
	
	//货品添加
	public function add(){
		if(IS_POST){
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('货品添加成功');
			
		}
	
	}
//	货品删除
	public function del(){
	   $hid = Q('get.hid',0,'intval');
	   $this->model->where("hid={$hid}")->delete();
		$str = <<<str
		<script>
		alert('删除成功');
		history.go(-1);
		</script>
str;
		echo $str;
	}
	
	
	
	//编辑货品
	public function edit(){
		
		if(IS_POST){
			$gid = Q('post.gid');
		
		   if(!$this->model->edit()) View::error($this->model->getError());
		   View::success('编辑成功',U('index',array('gid'=>"$gid")));    
		}
		$hid = Q('get.hid',0,'intval');
		//一：查询货品表总数据
		//获得货品列表里的goods_gid，我就知道了此货品属于哪个商品里，就知道里商品gid
		$gid = $this->model->where("hid={$hid}")->pluck('goods_gid');
		//获得商品表里的$type_typid
		$type_tid = Db::table('goods')->where("gid={$gid}")->pluck('type_tid');
		//根据$type_typid获得类型属性表里的tid , tvalue
		$tyattr = Db::table('nature')->where("type_tid={$type_tid} and natype=0")->get();
		//定义一个空数组，最后组合完的数组旧放到此数组里面
		$attr=array();
		foreach ($tyattr as $k => $v) {
			$arr['naname'] = $v['naname'];
			//循环组合数组
			$arr['nazhi'] = Db::table('goodssx')->where("nature_nid={$v['nid']}")->get();
			//最后把二个$arr小数组压入定义好的大数组里面
			$attr[] = $arr;
		}
		
		//p($tyattr);
		//p($attr);
		View::with('attr',$attr);
		//二：查询旧数据
		$oldData = $this->model->where("hid={$hid}")->find();
		//重组数组
		//p($oldData);
		$arr = explode('|', $oldData['groupid']);
		$oldData['groupid'] = $arr;
		//p($oldData);
		//分配数据
		View::with('oldData',$oldData);
	    View::make();
	}
	
	
	
	
	
}

	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
?>